import { sendMsg, orderConclude } from '@/events/index';

import BaseWs from './baseWs';

export default class ExSeoket extends BaseWs {
  /***************************覆盖方法****************************/
  onopen = () => {
    // 注册相关信息
    this.login();
    this.register();
  };

  onmessage = rs => {
    // if (rs.type == 'sendMsg') sendMsg.next(rs);
    // if (rs.type == 'orderConclude') orderConclude.next(rs);
  };

  /***************************业务相关方法(和其他ws不公用的)****************************/

  // 用户信息登录
  login(data) {
    const {
      config: { userId, uuid },
    } = this;
    this.sendMsg({
      type: 'login',
      data: { userId, uuid },
    });
  }

  // 注册一个类型的推送
  register(data) {
    const {
      config: { userId, uuid },
    } = this;

    this.sendMsg({
      type: 'register',
      data: {
        type: 'sendMsg',
        userId,
        uuid,
      },
    });

    this.sendMsg({
      type: 'register',
      data: {
        type: 'orderConclude',
        userId,
        uuid,
      },
    });
  }

  // 取消订阅一个类型的推送
  unsubscribe(data) {
    this.sendMsg({
      type: 'unsubscribe',
      data: {
        type: 'orderConclude',
        userId,
        uuid,
      },
    });
  }
}
